package nk.bfs;

import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;

public class AB20 {


    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] temp = in.readLine().split(" ");
        int n = Integer.parseInt(temp[0]);
        int m = Integer.parseInt(temp[1]);

        temp = in.readLine().split(" ");
        int x1 = Integer.parseInt(temp[0]) - 1;
        int y1 = Integer.parseInt(temp[1]) - 1;
        int x2 = Integer.parseInt(temp[2]) - 1;
        int y2 = Integer.parseInt(temp[3]) - 1;

        char[][] map = new char[n][];

        for (int i = 0; i < n; i++) {
            map[i] = in.readLine().toCharArray();
        }
        in.close();
        boolean[][] visited = new boolean[n][m];
        int step = 0;
        visited[x1][y1] = true;
        LinkedList<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x1, y1});
        int[] X = {-1, 1, 0, 0};
        int[] Y = {0, 0, -1, 1};
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                int x = queue.peek()[0];
                int y = queue.poll()[1];
                for (int i = 0; i < 4; i++) {
                    int nx = x + X[i];
                    int ny = y + Y[i];
                    if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny] && map[nx][ny] != '*') {
                        if (nx == x2 && ny == y2) {
                            System.out.println(step + 1);
                            return;
                        }
                        queue.offer(new int[]{nx, ny});
                        visited[nx][ny] = true;
                    }
                }
            }
            step++;
        }
        System.out.println(-1);
    }
}
